const app = getApp()
const util = require('../../../utils/util.js')

Page({

  data: {
    schoolList: [],
    userId: app.globalData.userId,
    hideScroll: true,
    isSupervising: false,
    isSuddenSupervising: false,
    inputValue: "",
    inputSchoolId: 0,
    scroolHeight: 0,
    // superviseChecked: [],
    superviseId: 0,
    superviseType: 0, // 1：专项巡查 2：定期巡查
    type: 0 // 1：定期专项巡查       2：日常巡查
  },
  onLoad: function (options) {
    console.log("options", options)
    if (options.type == 1) {
      this.setData({
        superviseId: options.superviseId,
        superviseType: options.superviseType
      })
      // this.setData({
      // superviseChecked: options.superviseChecked.split(',')
      // })

    }
    this.setData({
      type: options.type,
    })
  },

  selectSchool: function (e) {
    const {
      item
    } = e.currentTarget.dataset
    this.setData({
      inputValue: item.schoolname,
      hideScroll: true
    })
  },


  inputSchool: function (e) {
    let name = e.detail.value
    this.setData({
      inputValue: name
    })
    clearTimeout(this.data.timer)
    let timer = setTimeout(() => {
      if (name != null && name != "") {
        wx.request({
          url: app.inspectUrl + '/WeChat/weChatSystem/getSchoolList',
          method: "GET",
          data: {
            name: name,
            parent: app.globalData.departmentId
          },
          dataType: 'json',
          success: res => {
            if (res.data.length != 0) {
              let scroolHeight;
              if (res.data.length < 5) {
                scroolHeight = res.data.length * 100
              } else {
                scroolHeight = 400
              }
              this.setData({
                schoolList: res.data,
                hideScroll: false,
                scroolHeight: scroolHeight
              })
            }
          }
        })
      } else {
        this.setData({
          schoolList: [],
          hideScroll: true
        })
      }
    }, 600)
    this.setData({
      timer: timer
    })

  },

  judgeSchool: function (name, parent) {
    return new Promise((resolve, reject) => {
      wx.request({
        url: app.inspectUrl + '/WeChat/weChatSystem/judgeSchool',
        method: "POST",
        data: {
          name: name,
          parent: parent
        },
        success: res => {
          resolve(res)
        },
        fail: err => {
          reject(err)
        }
      })
    })
  },


  // confirmSelection: function (e) {
  //   let that = this
  //   let inputValue = that.data.inputValue
  //   if (inputValue == null || inputValue == "") {
  //     wx.showToast({
  //       title: '请输入学校名称！',
  //       icon: "none"
  //     })
  //   } else {
  //     wx.showModal({
  //       title: '提示',
  //       content: '确定选择该学校？',
  //       success: async function (res) {
  //         if (res.confirm) {
  //           const res = await that.judgeSchool(that.data.inputValue, app.globalData.departmentId)
  //           if (res.data.length == 0) {
  //             wx.showToast({
  //               title: '请输入正确的学校名称！',
  //               icon: "none"
  //             })
  //           } else {
  //             if (that.data.type == 1) {
  //               const schoolId = res.data[0].id
  //               let list = []
  //               let superviseIds = ''
  //               for (let i = 0; i < that.data.superviseChecked.length; i++) {
  //                 list.push({
  //                   userId: app.globalData.userId,
  //                   realname: app.globalData.realname,
  //                   superviseId: that.data.superviseChecked[i].split('-')[0],
  //                   type: that.data.superviseChecked[i].split('-')[1],
  //                   schoolId: schoolId,
  //                   schoolname: that.data.inputValue
  //                 })
  //                 superviseIds += that.data.superviseChecked[i].split('-')[0] + ','
  //               }
  //               superviseIds = superviseIds.substring(0, superviseIds.length - 1)
  //               wx.request({
  //                 url: app.baseUrl + '/WeChat/weChatSystem/startSupervise',
  //                 method: "POST",
  //                 data: list,
  //                 dataType: 'json',
  //                 success: res => {
  //                   app.globalData.isSupervising = true
  //                   app.globalData.superviseSchoolName = that.data.inputValue
  //                   app.globalData.superviseSchoolId = schoolId
  //                   app.globalData.superviseIds = superviseIds
  //                   wx.switchTab({
  //                     url: '/pages/safetysence/safetysence',
  //                   })
  //                 }
  //               })
  //             } else if (that.data.type == 2) {
  //               const schoolId = res.data[0].id
  //               wx.request({
  //                 url: app.baseUrl + '/WeChat/weChatSystem/startSuddenSupervise',
  //                 method: "POST",
  //                 data: {
  //                       userId: app.globalData.userId,
  //                       realname: app.globalData.realname,
  //                       schoolId: schoolId,
  //                       schoolname: that.data.inputValue
  //                     },
  //                 dataType: 'json',
  //                 success: res => {
  //                   app.globalData.isSuddenSupervising = true
  //                   app.globalData.suddensuperviseSchoolName = that.data.inputValue
  //                   app.globalData.suddensuperviseSchoolId = schoolId
  //                   wx.switchTab({
  //                     url: '/pages/safetysence/safetysence',
  //                   })
  //                 }
  //               })
  //             }
  //           }
  //         } else {
  //           console.log('取消')
  //         }
  //       }
  //     })
  //   }
  // },
  confirmSelection: function (e) {
    if (app.globalData.isSupervising == true) {
      wx.showToast({
        title: '请不要重复确定！',
        icon: 'none'
      })
    } else {
      let that = this
      let inputValue = that.data.inputValue
      if (inputValue == null || inputValue == "") {
        wx.showToast({
          title: '请输入学校名称！',
          icon: "none"
        })
      } else {
        wx.showModal({
          title: '提示',
          content: '确定选择该学校？',
          success: async function (res) {
            if (res.confirm) {
              const res = await that.judgeSchool(that.data.inputValue, app.globalData.departmentId)
              if (res.data.length == 0) {
                wx.showToast({
                  title: '请输入正确的学校名称！',
                  icon: "none"
                })
              } else {
                if (that.data.type == 1) {
                  const schoolId = res.data[0].id
                  wx.request({
                    url: app.inspectUrl + '/WeChat/weChatSystem/startSupervise',
                    method: "POST",
                    data: {
                      userId: app.globalData.userId,
                      realname: app.globalData.realname,
                      superviseId: that.data.superviseId,
                      type: that.data.superviseType,
                      schoolId: schoolId,
                      schoolname: that.data.inputValue
                    },
                    dataType: 'json',
                    success: res => {
                      if(res.data.res == "success") {
                        app.globalData.isSupervising = true
                        app.globalData.superviseSchoolName = that.data.inputValue
                        app.globalData.superviseSchoolId = schoolId
                        app.globalData.superviseId = res.data.id
                        wx.redirectTo({
                          url: '/subpages/pages/regularsupervise/regularsupervise',
                        })
                      } else {
                        wx.showToast({
                          title: '网络错误',
                          icon:'none'
                        })
                      }
                    }
                  })
                } else if (that.data.type == 2) {
                  const schoolId = res.data[0].id
                  wx.request({
                    url: app.inspectUrl + '/WeChat/weChatSystem/startSuddenSupervise',
                    method: "POST",
                    data: {
                      userId: app.globalData.userId,
                      realname: app.globalData.realname,
                      schoolId: schoolId,
                      schoolname: that.data.inputValue
                    },
                    dataType: 'json',
                    success: res => {
                      if(res.data.res == "success") {
                        app.globalData.isSuddenSupervising = true
                        app.globalData.suddensuperviseSchoolName = that.data.inputValue
                        app.globalData.suddensuperviseSchoolId = schoolId
                        app.globalData.suddensuperviseId = res.data.id
                        wx.switchTab({
                          url: '/pages/safetysence/safetysence',
                        })
                      }
                    }
                  })
                }
              }
            } else {
              console.log('取消')
            }
          }
        })
      }
    }

  },
})